<template>
  <div class="lang-container">
    <van-popover
      v-model:show="showPopover"
      class="lang-popover"
      theme="dark"
      :show-arrow="false"
    >
      <div
        class="lang"
        v-for="(item, index) in langList"
        :key="index"
        :text="item.title"
        @click="changeActive(item.title)"
      >
        <div class="lang-item">
          <span>{{ item.desc }}</span>
        </div>
      </div>
      <template #reference>
        <img src="@/assets/image/globe.png" alt="" class="img" />
      </template>
    </van-popover>
  </div>
</template>
<script setup lang="ts">
import { localStorage } from "@/utils/local-storage";
import { useI18n } from "vue-i18n";
const { locale } = useI18n();
const active = ref(locale.value || localStorage.get("lang"));
const langList = ref([
  {
    title: "zhtw",
    desc: "繁体中文"
  },
  {
    title: "en",
    desc: "English"
  }
  // {
  //   title: "zh",
  //   desc: "简体中文"
  // },
]);
const showPopover = ref(false);
const changeActive = lang => {
  locale.value = lang;
  active.value = lang;
  localStorage.set("lang", lang);
  showPopover.value = false;
  window.location.reload()
};
</script>
<style scoped lang="scss">
.lang-container {
  display: flex;
  align-items: center;
  .img {
    width: 60px;
    height: 30px;
  }
}
::v-deep .van-popover__content {
  border-radius: 4px !important;
  padding: 8px 0;
}
.lang-popover {
  background: transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  .lang {
    display: flex;
    justify-content: center;
    padding: 6px 16px;
    box-sizing: border-box;
    .lang-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      min-height: 36px;
      span {
        font-size: 1rem;
      }
    }
    &:last-child {
      margin-bottom: 0;
    }
  }
}
</style>
